<?php
/**
 * 管理员日志类
 * @author zengnz
 *
 */

class AdminLogAction extends CommonAction
{
	
	/**
	 * 操作日志
	 */
	public function actionLog() {
		$Model = M('log_admin_action');
		$this -> _list( $Model, array(), true, array('action_time'=>'DESC') );
	
		$this -> display();
	}
	
	/**
	 * 登录日志
	 */
	public function loginLog() {
		$Model = M('log_admin_login');
		$this -> _list( $Model, array(), true, array('login_time'=>'DESC') );
		
		$this -> display();
	}
	
	/**
	 * 登录日志线性图
	 */
	public function loginLogLine() {
		$Model = M('log_admin_login');
		
		$start_date = '2013-10-01';
		$end_date = '2013-11-01';
		if ($end_date > date('Y-m-d'))
			$end_date = date('Y-m-d', time()+24*3600);
		
		$days = ceil( ( strtotime($end_date) - strtotime($start_date) ) / 24 /3600 );
		
		$list = $Model -> where('login_time > "' . strtotime($start_date) . '" AND login_time < "' . strtotime($end_date) . '"') -> order('login_time ASC') -> select();
		$dayViewTimes = array();
		$dayViewIps = array();
		
		if ($list) {
			foreach ($list as $key => $record) {
				$date = substr($record['login_time'], 0, 10);
				$dayViewTimes[$date][] = $record;
				$dayViewIps[$date][] = $record['ip'];
			}
		}
		
		$xLine = '';
		$viewTimes = array();
		$viewIps = array();
		
		for($i=1; $i<=$days; $i++){
			$xLine[] = $i;
			$date = substr($start_date, 0, 7).'-'.$i;
			
			if ($i < 10)
				$date = substr($start_date, 0,7).'-0'.$i;
			
			$viewTimes[$i] = count($dayViewTimes[$date]);
			$viewIpTemp = array_unique($dayViewIps[$date]);
			if ($viewIpTemp)
				$viewIps[$i] = count($viewIpTemp);
			else
				$viewIps[$i] = 0;
		}
		
		$xLine = implode(',', $xLine);
		$max = max($viewTimes);
		$viewTimes = implode(',', $viewTimes);
		$viewIps = implode(',', $viewIps);
		
		$this -> assign('viewTimes', $viewTimes);
		$this -> assign('viewIps', $viewIps);
		$this -> assign('xLine', $xLine);
		$this -> assign('xNum', $days);
		$this -> assign('max', $max);
		
		$this -> display();
	}
	
	/**
	 * 扩展字段
	 * @param unknown_type $list
	 */
	protected function outputExt($list) {
		if ( $list ) {
			$types = LogModel::getTypes();
			foreach ( $list as $key => $val ) {
				if (isset($val['type']))
					$val['type'] = $types[$val['type']];
				if(isset($val['action_time']))
					$val['action_time'] = date('Y-m-d H:i:s', $val['action_time']);
				if (isset($val['login_time']))
					$val['login_time'] = date('Y-m-d H:i:s', $val['login_time']);
				$list[$key] = $val;
			}
		}
		
		return $list;
	}
	
}